﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LCL.CodeHelper
{
    public partial class CodeCommon
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="Folder"></param>
        public static void FolderCheck(string Folder)
        {
            DirectoryInfo info = new DirectoryInfo(Folder);
            if (!info.Exists)
            {
                info.Create();
            }
        }
        /// <summary>
        ///  创建UTF8文件
        /// </summary>
        /// <param name="path">创建路径</param>
        /// <param name="filescontent">文件内容</param>
        public static void CreateFiles(string path, string filescontent)
        {
            FolderCheck(Path.GetDirectoryName(path));
            var utf8WithoutBom = new UTF8Encoding(true);
            using (var sink = new StreamWriter(path, false, utf8WithoutBom))
            {
                sink.WriteLine(filescontent);
                sink.Flush();
                sink.Close();
                sink.Dispose();
            }
        }
        public static void CreateFiles(string path, string filescontent, Encoding encoding)
        {
            FolderCheck(Path.GetDirectoryName(path));
            using (var sink = new StreamWriter(path, false, encoding))
            {
                sink.WriteLine(filescontent);
                sink.Flush();
                sink.Close();
                sink.Dispose();
            }
        }
        /// <summary>
        /// 当前用户桌面路径
        /// </summary>
        public static string DesktopDirectory
        {
            get
            {
                string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "/LCL/";
                FolderCheck(dir);
                return dir;
            }
        }
        public static T GetObjTranNull<T>(object obj)
        {
            try
            {
                if (obj == null)
                {
                    return (T)System.Convert.ChangeType("", typeof(T));
                }
                else
                {
                    if (obj.GetType() == typeof(T))
                        return (T)obj;
                }

                return (T)System.Convert.ChangeType(obj, typeof(T));
            }
            catch
            {

            }
            return default(T);
        }
        /// <summary>
        ///生成制定位数的随机码（数字）
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateRandomCode(int length = 5)
        {
            var result = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }
        /// <summary>
        /// 复制目录
        /// </summary>
        /// <param name="SourceDirectory"></param>
        /// <param name="TargetDirectory"></param>
        public static void CopyDirectory(string SourceDirectory, string TargetDirectory)
        {
            DirectoryInfo source = new DirectoryInfo(SourceDirectory);
            DirectoryInfo target = new DirectoryInfo(TargetDirectory);
            //Check If we have valid source
            if (!source.Exists)
                return;
            if (!target.Exists)
                target.Create();
            //Copy Files
            FileInfo[] sourceFiles = source.GetFiles();
            int filescount = sourceFiles.Length;
            for (int i = 0; i < filescount; ++i)
            {
                if ((sourceFiles[i].Extension == ".sln") || (sourceFiles[i].Extension == ".suo"))
                {
                    File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Extension, true);
                }
                else
                {
                    File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
                }
            }
            //Copy directories
            DirectoryInfo[] sourceDirectories = source.GetDirectories();
            for (int j = 0; j < sourceDirectories.Length; ++j)
            {
                CopyDirectory(sourceDirectories[j].FullName, target.FullName + "\\" + sourceDirectories[j].Name);
            }
        }
        /// <summary>
        /// 只保留字母、数字 和汉字
        /// </summary>
        public static string FilterChar(string inputValue)
        {
            string strAfter = Regex.Replace(inputValue, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
            return strAfter;
        }
    }
}
